package com.yijiaw.dao;

import com.yijiaw.domain.Us;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Us entities.
 * 
 */
@Repository("UsDAO")
@Transactional
public class UsDAOImpl extends AbstractJpaDao<Us> implements UsDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Us.class }));

	/**
	 * EntityManager injected by Spring for persistence unit yijiaw
	 *
	 */
	@PersistenceContext(unitName = "yijiaw")
	private EntityManager entityManager;

	/**
	 * Instantiates a new UsDAOImpl
	 *
	 */
	public UsDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findAllUss
	 *
	 */
	@Transactional
	public Set<Us> findAllUss() throws DataAccessException {

		return findAllUss(-1, -1);
	}

	/**
	 * JPQL Query - findAllUss
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Us> findAllUss(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllUss", startResult, maxRows);
		return new LinkedHashSet<Us>(query.getResultList());
	}

	/**
	 * JPQL Query - findUsById
	 *
	 */
	@Transactional
	public Us findUsById(Integer id) throws DataAccessException {

		return findUsById(id, -1, -1);
	}

	/**
	 * JPQL Query - findUsById
	 *
	 */

	@Transactional
	public Us findUsById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findUsById", startResult, maxRows, id);
			return (com.yijiaw.domain.Us) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findUsByAddress
	 *
	 */
	@Transactional
	public Set<Us> findUsByAddress(String address) throws DataAccessException {

		return findUsByAddress(address, -1, -1);
	}

	/**
	 * JPQL Query - findUsByAddress
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Us> findUsByAddress(String address, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUsByAddress", startResult, maxRows, address);
		return new LinkedHashSet<Us>(query.getResultList());
	}

	/**
	 * JPQL Query - findUsByContactContaining
	 *
	 */
	@Transactional
	public Set<Us> findUsByContactContaining(String contact) throws DataAccessException {

		return findUsByContactContaining(contact, -1, -1);
	}

	/**
	 * JPQL Query - findUsByContactContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Us> findUsByContactContaining(String contact, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUsByContactContaining", startResult, maxRows, contact);
		return new LinkedHashSet<Us>(query.getResultList());
	}

	/**
	 * JPQL Query - findUsByContact
	 *
	 */
	@Transactional
	public Set<Us> findUsByContact(String contact) throws DataAccessException {

		return findUsByContact(contact, -1, -1);
	}

	/**
	 * JPQL Query - findUsByContact
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Us> findUsByContact(String contact, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUsByContact", startResult, maxRows, contact);
		return new LinkedHashSet<Us>(query.getResultList());
	}

	/**
	 * JPQL Query - findUsByNameContaining
	 *
	 */
	@Transactional
	public Set<Us> findUsByNameContaining(String name) throws DataAccessException {

		return findUsByNameContaining(name, -1, -1);
	}

	/**
	 * JPQL Query - findUsByNameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Us> findUsByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUsByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Us>(query.getResultList());
	}

	/**
	 * JPQL Query - findUsByEmailContaining
	 *
	 */
	@Transactional
	public Set<Us> findUsByEmailContaining(String email) throws DataAccessException {

		return findUsByEmailContaining(email, -1, -1);
	}

	/**
	 * JPQL Query - findUsByEmailContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Us> findUsByEmailContaining(String email, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUsByEmailContaining", startResult, maxRows, email);
		return new LinkedHashSet<Us>(query.getResultList());
	}

	/**
	 * JPQL Query - findUsByEmail
	 *
	 */
	@Transactional
	public Set<Us> findUsByEmail(String email) throws DataAccessException {

		return findUsByEmail(email, -1, -1);
	}

	/**
	 * JPQL Query - findUsByEmail
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Us> findUsByEmail(String email, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUsByEmail", startResult, maxRows, email);
		return new LinkedHashSet<Us>(query.getResultList());
	}

	/**
	 * JPQL Query - findUsByPrimaryKey
	 *
	 */
	@Transactional
	public Us findUsByPrimaryKey(Integer id) throws DataAccessException {

		return findUsByPrimaryKey(id, -1, -1);
	}

	/**
	 * JPQL Query - findUsByPrimaryKey
	 *
	 */

	@Transactional
	public Us findUsByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findUsByPrimaryKey", startResult, maxRows, id);
			return (com.yijiaw.domain.Us) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findUsByFax
	 *
	 */
	@Transactional
	public Set<Us> findUsByFax(String fax) throws DataAccessException {

		return findUsByFax(fax, -1, -1);
	}

	/**
	 * JPQL Query - findUsByFax
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Us> findUsByFax(String fax, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUsByFax", startResult, maxRows, fax);
		return new LinkedHashSet<Us>(query.getResultList());
	}

	/**
	 * JPQL Query - findUsByPhoneContaining
	 *
	 */
	@Transactional
	public Set<Us> findUsByPhoneContaining(String phone) throws DataAccessException {

		return findUsByPhoneContaining(phone, -1, -1);
	}

	/**
	 * JPQL Query - findUsByPhoneContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Us> findUsByPhoneContaining(String phone, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUsByPhoneContaining", startResult, maxRows, phone);
		return new LinkedHashSet<Us>(query.getResultList());
	}

	/**
	 * JPQL Query - findUsByName
	 *
	 */
	@Transactional
	public Set<Us> findUsByName(String name) throws DataAccessException {

		return findUsByName(name, -1, -1);
	}

	/**
	 * JPQL Query - findUsByName
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Us> findUsByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUsByName", startResult, maxRows, name);
		return new LinkedHashSet<Us>(query.getResultList());
	}

	/**
	 * JPQL Query - findUsByFaxContaining
	 *
	 */
	@Transactional
	public Set<Us> findUsByFaxContaining(String fax) throws DataAccessException {

		return findUsByFaxContaining(fax, -1, -1);
	}

	/**
	 * JPQL Query - findUsByFaxContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Us> findUsByFaxContaining(String fax, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUsByFaxContaining", startResult, maxRows, fax);
		return new LinkedHashSet<Us>(query.getResultList());
	}

	/**
	 * JPQL Query - findUsByAddressContaining
	 *
	 */
	@Transactional
	public Set<Us> findUsByAddressContaining(String address) throws DataAccessException {

		return findUsByAddressContaining(address, -1, -1);
	}

	/**
	 * JPQL Query - findUsByAddressContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Us> findUsByAddressContaining(String address, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUsByAddressContaining", startResult, maxRows, address);
		return new LinkedHashSet<Us>(query.getResultList());
	}

	/**
	 * JPQL Query - findUsByPhone
	 *
	 */
	@Transactional
	public Set<Us> findUsByPhone(String phone) throws DataAccessException {

		return findUsByPhone(phone, -1, -1);
	}

	/**
	 * JPQL Query - findUsByPhone
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Us> findUsByPhone(String phone, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findUsByPhone", startResult, maxRows, phone);
		return new LinkedHashSet<Us>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Us entity) {
		return true;
	}
}
